package member.base.service;

import m.common.service.Service;
import m.system.db.DataRow;
import m.system.db.SqlBuffer;
import m.system.util.DateUtil;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PageService extends Service {
    /**
     * 清除店铺相关缓存
     */
    public void clear(String storeOid){
        clearOrder(storeOid);
        clearRecharge(storeOid);
        clearMonth(storeOid);
    }

    private final static Map<String, Map<String,Object>> orderMap=new HashMap<>();
    public void clearOrder(String storeOid){
        orderMap.remove(storeOid);
    }
    public Map<String, Object> getOrder(String storeOid) throws SQLException {
        if(null==orderMap.get(storeOid)) {
            Map<String,Object> map=new HashMap<>();
            DataRow dr = new SqlBuffer()
                .append("select")
                .append("sum(if(status='N',1,0)) confirmNum,")
                .append("sum(if(status='N',pay_amount,0)) confirmAmount,")
                .append("sum(if(status='Y',1,0)) doneNum,")
                .append("sum(if(status='Y',pay_amount,0)) doneAmount")
                .append("from o_order_info")
                .append("where input_store_oid=?", storeOid)
                .queryFirstRow();
            if (null != dr) {
                map.put("confirmNum", dr.get("confirmNum"));
                map.put("confirmAmount", dr.get("confirmAmount"));
                map.put("doneNum", dr.get("doneNum"));
                map.put("doneAmount", dr.get("doneAmount"));
            }
            orderMap.put(storeOid,map);
        }
        return orderMap.get(storeOid);
    }
    private final static Map<String, Map<String,Object>> rechargeMap=new HashMap<>();
    public void clearRecharge(String storeOid){
        rechargeMap.remove(storeOid);
    }
    public Map<String, Object> getRecharge(String storeOid) throws SQLException {
        if(null==rechargeMap.get(storeOid)) {
            Map<String,Object> map=new HashMap<>();
            DataRow dr = new SqlBuffer()
                .append("select")
                .append("sum(if(status='N',1,0)) confirmNum,")
                .append("sum(if(status='N',pay_amount,0)) confirmAmount,")
                .append("sum(if(status='Y',1,0)) doneNum,")
                .append("sum(if(status='Y',pay_amount,0)) doneAmount")
                .append("from o_member_recharge")
                .append("where input_store_oid=?", storeOid)
                .queryFirstRow();
            if (null != dr) {
                map.put("confirmNum", dr.get("confirmNum"));
                map.put("confirmAmount", dr.get("confirmAmount"));
                map.put("doneNum", dr.get("doneNum"));
                map.put("doneAmount", dr.get("doneAmount"));
            }
            rechargeMap.put(storeOid,map);
        }
        return rechargeMap.get(storeOid);
    }
    private final static Map<String, Map<String,Object>> monthMap=new HashMap<>();
    public void clearMonth(String storeOid){
        monthMap.remove(storeOid);
    }
    public Map<String, Object> getMonth(String storeOid) throws SQLException {
        if(null==monthMap.get(storeOid)) {
            Map<String,Object> map=new HashMap<>();
            String month= DateUtil.format(new Date(),"yyyy-MM");
            map.put("month",month);
            DataRow dr = new SqlBuffer()
                .append("select")
                .append("sum(order_num) orderNum,")
                .append("sum(order_amount) orderAmount,")
                .append("sum(preferential_amount) preferentialAmount,")
                .append("sum(member_day_amount) memberDayAmount,")
                .append("sum(recharge_num) rechargeNum,")
                .append("sum(recharge_amount) rechargeAmount")
                .append("from o_order_collect")
                .append("where input_store_oid=?", storeOid)
                .append("and left(account_date,7)=?",month)
                .queryFirstRow();
            if (null != dr) {
                map.put("orderNum", dr.get("orderNum"));
                map.put("orderAmount", dr.get(Double.class,"orderAmount"));
                map.put("preferentialAmount", dr.get("preferentialAmount"));
                map.put("memberDayAmount", dr.get("memberDayAmount"));
                map.put("rechargeNum", dr.get("rechargeNum"));
                map.put("rechargeAmount", dr.get("rechargeAmount"));
            }
            String[] ym=month.split("-");
            dr = new SqlBuffer()
                .append("select")
                .append("plan"+ym[1]+" planAmount")
                .append("from o_store_plan")
                .append("where input_store_oid=?", storeOid)
                .append("and plan_year=?",ym[0])
                .queryFirstRow();
            if (null != dr) {
                Double pa=null!=dr.get("planAmount")?dr.get(Double.class,"planAmount"):0d;
                Double oa=null!=map.get("orderAmount")?(Double)map.get("orderAmount"):0d;
                map.put("planAmount", pa);
                map.put("completionRate",oa>pa||pa==0?100d:(oa/pa*100.0));
            }else{
                map.put("planAmount", 0d);
                map.put("completionRate",100d);
            }
            monthMap.put(storeOid,map);
        }
        return monthMap.get(storeOid);
    }
}
